package com.zhy.baselibrary.view;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.zhy.baselibrary.R;


@SuppressLint("InflateParams")
public class LoadingDialog extends Dialog {

    private static final int theme = R.style.loadingDialog;
    private Context context;
    private TextView msg;
    private CircularBar circle;
    private TextView tvProgress;

    public LoadingDialog(Context context) {
        super(context, theme);
        this.context = context;
        init();
        setMessage(null);
    }

    public LoadingDialog(Context context, String message) {
        super(context, theme);
        this.context = context;
        init();
        setMessage(message);
    }

    public LoadingDialog(Context context, int message) {
        super(context, theme);
        this.context = context;
        init();
        setMessage(message);
    }

    @SuppressLint("MissingInflatedId")
    private void init() {
        View contentView = LayoutInflater.from(context).inflate(R.layout.dialog_loading, null);
        setContentView(contentView);
        setCanceledOnTouchOutside(false);
        circle = contentView.findViewById(R.id.circle);
        circle.setFrontColor(Color.parseColor("#ff0000"));
        circle.startAnimation();
        msg = contentView.findViewById(R.id.tv_msg);
    }


    public void setMessage(String message) {
        if (message == null) {
            if (msg.getVisibility() == View.VISIBLE)
                msg.setVisibility(View.GONE);
            return;
        }

        if (msg.getVisibility() == View.GONE)
            msg.setVisibility(View.VISIBLE);
        msg.setText(message);
    }

    public void setMessage(int message) {
        if (msg.getVisibility() == View.GONE)
            msg.setVisibility(View.VISIBLE);
        msg.setText(message);
    }

    public void setProgress(String progress) {
//        if (TextUtils.isEmpty(progress)) {
//            tvProgress.setVisibility(View.GONE);
//        } else {
//            tvProgress.setVisibility(View.VISIBLE);
//            tvProgress.setText(progress);
//        }

    }

    @Override
    public void show() {
        super.show();
    }

    @Override
    public void dismiss() {
        if (circle != null) {
            circle.stopAnimation();
        }
        super.dismiss();
    }
}
